<template>
  <div>
    <pageMain padding="5px " background="transparent">
      <div class="chunk">
        <el-form
          :inline="true"
          class="demo-form-inline"
          size="mini"
          label-width="80px"
          label-position="left"
        >
          <el-form-item label="用户名">
            <el-input
              v-model="pageData.where.username"
              placeholder="请输入搜索用户名"
            ></el-input>
          </el-form-item>
          <el-form-item label="昵称">
            <el-input
              v-model="pageData.where.nickname"
              placeholder="请输入搜索昵称"
            ></el-input>
          </el-form-item>
          <el-form-item label="手机号">
            <el-input
              v-model="pageData.where.mobile"
              placeholder="请输入手机号码"
            ></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="pageData.where.state" placeholder="用户状态">
              <el-option label="不限" :value="null" :key="null"></el-option>
              <el-option
                v-for="state in stateList"
                :label="state.name"
                :value="state.value"
                :key="state.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="角色">
            <el-select v-model="pageData.where.role" placeholder="用户角色">
              <el-option
                v-for="role in rolesList"
                :label="role.name"
                :value="role.id"
                :key="role.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="性别">
            <el-select v-model="pageData.where.gender" placeholder="用户性别">
              <el-option
                v-for="gender in genderList"
                :label="gender.name"
                :value="gender.value"
                :key="gender.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="clickWhere">查询</el-button>
            <el-button type="primary" @click="clickReset">重置</el-button>
            <el-button type="primary" @click="clickAddUser"
              >添加新用户</el-button
            >
          </el-form-item>
        </el-form>

        <el-empty v-if="!tableData.length"></el-empty>

        <el-table
          border
          v-else
          :data="tableData"
          style="width: 100%"
          fit
          size="small"
        >
          <el-table-column type="selection" width="55" align="center">
          </el-table-column>
          <el-table-column prop="date" label="头像" align="center">
            <template slot-scope="scope">
              <div
                class="overall-flex overall-align-center overall-justify-center"
              >
                <avatar :size="34" :src="scope.row.avatar"></avatar>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="username" label="用户名" width="180">
            <template slot-scope="scope">
              <el-tooltip
                class="item"
                effect="dark"
                :content="scope.row.username"
                placement="top-start"
              >
                <span class="username-span">{{ scope.row.username }}</span>
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column
            prop="gender"
            label="性别"
            align="center"
          ></el-table-column>
          <el-table-column prop="roles" label="角色"></el-table-column>
          <el-table-column prop="mobile" label="手机号" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.contact.mobile || "未绑定" }}</span>
            </template>
          </el-table-column>
          <el-table-column label="邮箱" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.contact.email || "未绑定" }}</span>
            </template>
          </el-table-column>
          <el-table-column label="QQ" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.contact.qq || "未绑定" }}</span>
            </template>
          </el-table-column>
          <el-table-column label="微信" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.contact.wechat || "未绑定" }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="register_info.date"
            label="注册方式"
          ></el-table-column>
          <el-table-column
            prop="register_info.date"
            label="注册时间"
          ></el-table-column>
          <el-table-column
            prop="register_info.ip"
            label="注册IP"
          ></el-table-column>
          <el-table-column
            prop="register_info.ip"
            label="注册IP所在地"
          ></el-table-column>
          <el-table-column
            prop="register_info.diff_now_day"
            label="注册天数"
            align="center"
          ></el-table-column>
          <el-table-column prop="name" label="最近登录时间"></el-table-column>
          <el-table-column
            prop="last_login_info.ip"
            label="最近登录IP"
          ></el-table-column>
          <el-table-column
            prop="last_login_info.ip"
            label="最近登录IP所在地"
          ></el-table-column>
          <el-table-column prop="name" label="所在地区"></el-table-column>
          <el-table-column
            prop="state"
            label="状态"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="address"
            fixed="right"
            label="操作"
            width="190"
            align="center"
          >
            <template slot="header">
              <div
                class="overall-flex overll-align-center"
                style="align-items: center"
              >
                <p class="overall-flex-1 overall-text-center">操作</p>

                <i class="el-icon-s-tools"></i>
              </div>
            </template>
            <template slot-scope="scope">
              <div class="user-options overall-flex">
                <el-link
                  type="warning"
                  @click="clickLookUser(scope.row, scope.$index)"
                  >查看</el-link
                >
                <el-link
                  type="primary"
                  @click="clickEditUser(scope.row, scope.$index)"
                  >编辑</el-link
                >
                <el-link
                  type="danger"
                  @click="clickRemoveUser(scope.row, scope.$index)"
                  >删除</el-link
                >
              </div>
            </template></el-table-column
          >
        </el-table>

        <page-pagination></page-pagination>

        <div class="pagination">
          <el-pagination
            background
            layout="prev, pager, next"
            :current-page.sync="pageData.page"
            :total="pageData.count"
            :page-size="pageData.limit"
            @current-change="getUserList"
          >
          </el-pagination>
        </div>
      </div>

      <el-dialog
        title="编辑用户信息"
        :visible.sync="editDialogShow"
        :modal-append-to-body="false"
      >
        <div class="edit-user">
          <el-form
            ref="form"
            :model="editUserForm"
            label-width="80px"
            label-position="left"
          >
            <el-form-item label="头像">
              <el-input v-model="editUserForm.username"></el-input>
            </el-form-item>
            <el-form-item label="用户名">
              <el-input v-model="editUserForm.username"></el-input>
            </el-form-item>
            <el-form-item label="昵称">
              <el-input v-model="editUserForm.nickname"></el-input>
            </el-form-item>
            <el-form-item label="性别">
              <el-select v-model="editUserForm.gender" placeholder="请选择">
                <el-option
                  v-for="gender in genderList"
                  :label="gender.name"
                  :value="gender.value"
                  :key="gender.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="个性签名">
              <el-input type="textarea" v-model="editUserForm.desc"></el-input>
            </el-form-item>
            <el-form-item label="角色">
              <el-checkbox-group v-model="editUserForm.type">
                <el-checkbox
                  v-for="(role, index) in rolesList"
                  :label="role.name"
                  :name="role.id"
                  :key="index"
                ></el-checkbox>
              </el-checkbox-group>
            </el-form-item>
            <el-form-item label="备注">
              <el-input type="textarea" v-model="editUserForm.desc"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary">保存</el-button>
              <el-button>取消</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-dialog>
    </pageMain>
  </div>
</template>

<script>
// import pageDialog from "~/page-dialog/page-dialog.vue";

import pagePagination from "~/page-pagination/page-pagination.vue";
import pageMain from "~/page-main/page-main.vue";
import avatar from "~/avatar/avatar.vue";
import API from "@/api";
export default {
  components: {
    pageMain,
    pagePagination,
    // pageDialog,
    avatar,
  },
  methods: {
    // 点击查看用户
    clickLookUser(item, index) {
      console.log(item, index);

      this.$router.push({
        name: "userManagementInfo",
        query: {
          userid: item.id,
        },
      });
    },
    getRoleList() {
      return API.roles.list().then((res) => {
        let { data } = res;

        console.log(data);
        this.rolesList.push(...data.list);
      });
    },
    clickWhere() {
      this.pageData.page = 1;
      this.getUserList();
    },
    clickReset() {
      this.pageData.page = 1;
      this.pageData.where.username = "";
      this.pageData.where.nickname = "";
      this.pageData.where.mobile = null;
      this.pageData.where.state = null;
      this.pageData.where.role = null;
      this.pageData.where.gender = null;
      this.getUserList();
    },
    clickAddUser() {
      this.$router.push({
        name: "userManagementAdd",
      });
    },

    getUserList() {
      let { page, limit, where } = this.pageData;
      API.user
        .list({ page, limit, ...where })
        .then((res) => {
          console.log(res);
          let { data } = res;
          this.tableData = [];
          this.tableData = data.list;
          let { count, limit } = data;
          this.pageData.limit = limit;
          this.pageData.count = count;
        })
        .catch((err) => {
          console.log(err);
          this.tableData = [];
          this.$message({
            message: err.msg,
            type: "error",
          });
        });
    },

    // 点击编辑用户
    clickEditUser(item, index) {
      let { username, gender } = item;
      this.editUserForm.username = username;
      this.editUserForm.gender = gender;
      this.editDialogShow = true;
      console.log("角色列表：", this.rolesList);
      console.log(item, index);
      // this.$router.push("/user/look");
    },
    // 点击删除用户
    clickRemoveUser(item, index) {
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return API.user.Remove({ id: item.id });
        })
        .then((res) => {
          console.log(res);
          this.$message({
            message: res.msg,
            type: "success",
          });
          this.tableData.splice(index, 1);
        })
        .catch((err) => {
          if (typeof err != "string") {
            this.$message({
              message: err.msg,
              type: "error",
            });
          }
        });
    },

    genderCommand(value) {
      this.pageData.gender = value;
      this.getUserList();
    },
    stateCommand(value) {
      this.pageData.state = value;
      this.getUserList();
    },
  },
  created() {
    this.getRoleList();
    this.getUserList();
  },
  data() {
    return {
      editDialogShow: false,
      editUserForm: {
        name: "",
        region: "",
        date1: "",
        date2: "",
        delivery: false,
        type: [],
        resource: "",
        desc: "",
        gender: 0, // 性别
        username: "", // 用户名
        nickname: "", // 昵称
      },
      rolesList: [],
      genderList: [
        {
          name: "保密",
          value: 0,
        },
        {
          name: "男",
          value: 1,
        },
        {
          name: "女",
          value: 2,
        },
      ],
      stateList: [
        {
          name: "正常",
          value: 1,
        },
        {
          name: "暂停",
          value: 2,
        },
      ],
      searchValue: "",
      pageData: {
        page: 1,
        limit: 10,
        count: null,
        where: {
          mobile: "",
          username: "",
          nickname: "",
          gender: null,
          state: null,
          role: null,
        },
      },

      tableData: [],
    };
  },
};
</script>

<style lang="scss" scoped>
.chunk {
  background: white;
  margin-bottom: 20px;
  padding: 14px 14px;
}
.pagination {
  margin: 10px 0px;
  margin-top: 15px;
  display: flex;
  justify-content: center;
}
.user-options {
  justify-content: space-around;
}
.where-list {
  gap: 35px;
  margin-bottom: 10px;
}

.username-span {
  white-space: nowrap;
  overflow: hidden;
}
</style>
